package com.ali.groupchat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Scanner;

/**
 * 自定义客户端处理器
 */
public class MyGroupChatClientHandler extends SimpleChannelInboundHandler<String> {
    /**
     * 通道激活
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        new Thread(() -> {
            //构建用户输入
            Scanner scanner = new Scanner(System.in);
            //检测是否输入
            while (scanner.hasNext()) {
                //获取用户输入
                String message = scanner.next();
                ctx.write(message);
                ctx.flush();
            }
        }).start();
    }

    /**
     * 通道读取
     */
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) {
        System.out.println(s);
    }
}
